import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: ThemeWidget(),
  ));
}

class ThemeWidget extends StatefulWidget {

  @override
  _ThereWidget createState() => _ThereWidget();
}

class _ThereWidget extends State<ThemeWidget> {

  var _themeColor = Colors.teal;

  @override
  Widget build(BuildContext context) {
    ThemeData themeData = Theme.of(context);
    return Theme(
      data: ThemeData(
        primarySwatch: _themeColor, //用于导航栏、FloatingActionButton的背景色等
        iconTheme: IconThemeData(color: _themeColor) //用于Icon颜色
      ),
      child: Scaffold (
        appBar: AppBar(title: const Text("主题颜色"),),
        body: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            //第一行Icon使用主题中的iconTheme
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: const <Widget>[
                Icon(Icons.favorite),
                Icon(Icons.airport_shuttle),
                Text("  颜色跟随主题"),
              ],
            ),
            //为第二行Icon自定义颜色（固定为黑色)
            Theme(
              data: themeData.copyWith(
                iconTheme: themeData.iconTheme.copyWith(
                  color: Colors.black
                ),
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: const <Widget>[
                  Icon(Icons.favorite),
                  Icon(Icons.airport_shuttle),
                  Text("  颜色固定黑色"),
                ],
              ),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Container(
                  color: Colors.white,
                  child: const Icon(Icons.ac_unit, color: Colors.red,),
                ),
                const Text("  颜色指定红色"),
              ],
            ),
          ],
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () => setState((){
            _themeColor = _themeColor == Colors.teal ? Colors.blue : Colors.teal;
          }),
          child: const Icon(Icons.palette),
        ),
      ),
    );
  }
}